package com.lark.algorithm.train.onChapter.linkedList;

import com.lark.algorithm.common.domain.letcode.ListNode;

/**
 * @author btmood
 * @version 1.0
 * @apiNote
 * @since 2024-05-30 16:27
 */
public class D20240530Q61Q82_删除排序链表中的重复元素II {

    /**
     * 递归
     * 失败
     * @param head
     * @return
     */
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode nextNode = head.next;
        if (head.val == nextNode.val) {
            return deleteDuplicates(nextNode);
        } else {
            head.next = deleteDuplicates(nextNode);
            return head;
        }
    }

    public ListNode deleteDuplicates2(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode curNode = head;
        boolean isChange = false;
        while (curNode.next != null && head.val == head.next.val) {
            isChange = true;
            curNode = curNode.next;
        }
        if (isChange) {
            return deleteDuplicates2(curNode.next);
        } else {
            curNode.next = deleteDuplicates2(curNode.next);
            return curNode;
        }
    }

}
